import BadRoomStepView from "./BadRoomStepView";
import { ResultState } from "../../../sdk/SDKConfig";
import SDKManager from "../../../sdk/SDKManager";
import CollisionHelper from "../../../cfw/collide/CollisionHelper";
import { EventName, GameText } from "../../../public/Config";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import SleepToyItemModel from "../model/SleepToyItemModel";
import BadRoomManager from "../model/BadRoomManager";
import TipController from "../../tip/TipController";
import DragItemView from "../../item/DragItemView";
import LayerMgr from "../../../engine/LayerMgr";



const { ccclass, property } = cc._decorator;

@ccclass
export default class BadRoomStepView1 extends BadRoomStepView {

    @property([cc.Node])
    posList: cc.Node[] = []

    @property(cc.Node)
    targetPos: cc.Node = null;

    protected model: BadRoomManager;
    protected initPos: cc.Vec3 = cc.v3()
    private clickIndex: number = -1;
    protected daoju: DragItemView[] = []

    delegate(layerMgr: LayerMgr, index: number, item: cc.Node) {
        // cc.log(' delegate index ', index)
        let list: SleepToyItemModel[] = this.model.getList()
        let model = list[index]
        let comp = this.controller.initView(item, 'DragItemView', model) as DragItemView
        comp.updatePosition(this.posList[index].getPosition())
        // if (comp.isLoad()) {
        this.daoju[index] = comp;
        comp.refresh()
        // }
    }

    start() {
        this.model = BadRoomManager.instance();
        // let list: SleepToyItemModel[] = this.model.getList()
        // for (let index = 0; index < this.daoju.length; index++) {
        //     const element = this.daoju[index];
        //     if (list[index]) {
        //         element.setModel(list[index])
        //     }
        // }
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this)
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this)
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this)
    }
    touchStart(e: cc.Touch) {
        if (this.clickIndex == -1) {
            let pos = e.getLocation();
            for (let index = 0; index < this.daoju.length; index++) {
                const element = this.daoju[index];
                if (element.getBoundingBoxToWorld().contains(pos)) {
                    if (!element.isOpen()) {
                        TipController.instance().showToast(GameText.ITEM_TOAST, { num: element.getOpenLevel() })
                        return;
                    }
                    cc.log(' element.needBuy() ', element.needBuy())
                    if (element.needBuy()) {
                        this.controller.openItem(element.getModel())

                    } else {
                        this.clickIndex = index;
                        this.initPos.x = this.daoju[this.clickIndex].x;
                        this.initPos.y = this.daoju[this.clickIndex].y;
                        cc.log(' 点击到 是 index ', this.clickIndex)

                    }


                    break;
                }
            }
        }
    }
    touchMove(e: cc.Touch) {
        if (this.clickIndex >= 0) {
            let pos = e.getLocation();
            pos = this.node.convertToNodeSpaceAR(pos)
            this.daoju[this.clickIndex].x = pos.x;
            this.daoju[this.clickIndex].y = pos.y;

        }

    }

    touchEnd() {
        if (this.clickIndex >= 0) {
            let rect1 = this.daoju[this.clickIndex].node.getBoundingBox()
            let rect2 = this.targetPos.getBoundingBox()
            if (CollisionHelper.isRectCollideRect(rect1, rect2)) {
                this.daoju[this.clickIndex].node.x = this.targetPos.x;
                this.daoju[this.clickIndex].node.y = this.targetPos.y;
                this.model.addNum(this.daoju[this.clickIndex].getModel())
                for (let index = 0; index < this.daoju.length; index++) {
                    const element = this.daoju[index];
                    if (index != this.clickIndex) {
                        element.setVisible(false)
                    }
                }

                GlobalEvent.instance().emit(EventName.UPDATE_SLEEP_STEP, 1)
            } else {
                this.daoju[this.clickIndex].x = this.initPos.x;
                this.daoju[this.clickIndex].y = this.initPos.y;
            }
            this.clickIndex = -1;
        }

    }
}
